/*
 * Author: Christopher Yeaman
 * Project: ManuscriptPR
 * Class: TCSS 360, Tenenberg
 * Date: Autumn 2011
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

import user.Conference;
import user.Role;
import user.User;
import user.UserCollection;
import document.Manuscript;
import document.ManuscriptCollection;

/**
 * The panel for the PCViews user story. This panel should only be accessible by the
 * Program chair and will be handled and checked by the GUI.
 * 
 * @author Christopher Yeaman
 * @author Tony Butterfield (Assisting with Javadoc and Declared fields).
 * @version 12/4/2012
 */
@SuppressWarnings("serial")
public class PCViewsPanel2 extends JPanel
{
  // INSTANCE FIELDS
  
  /** The current Conference selected.  */
  private Conference my_conference;
  
  /**  the parent frame.   */
  private MainFrame1 my_parent;
  
  /** The collection of manuscripts associated with the current conference.   */
  private ManuscriptCollection my_manuscripts;

  /** The users associated with the conference.   */
  private UserCollection my_users;
  
  /**
   * The current user.
   */
  private User my_user;
  
  /**
   * The current role of the user.
   */
  private Role my_role;

  /**
   * Constructor.
   * 
   * @param the_parent the main frame of the gui.
   */
  public PCViewsPanel2(final MainFrame1 the_parent)
  {
    super();
    my_parent = the_parent;
    my_users = my_parent.getUserCollection();
    my_manuscripts = my_parent.getManuscriptCollection();
    refresh();
  
    //my_parent.pack();
    //setup();
  }
  
  /**
   * call to get current data from the Frame.
   * @author Tony Butterfield <b>Edited by:</b> Christopher Yeaman
   */
  public void refresh()
  {
    setVisible(true);
    my_user = my_parent.getCurrentUser();
    my_conference = my_parent.getCurrentConference();
    my_role = my_parent.getCurrentRole();
    if (my_role != Role.PROGRAM_CHAIR)
    {
      JOptionPane.showMessageDialog(null, "This user is not a Program Chair.");
      setVisible(false);
    }
    setup();
    my_parent.pack();
  }

  /**
   * Puts the category labels at the top of the panel.
   */
  public void setup()
  {
    
    setLayout(new BorderLayout());
    final String [] col_names = {"Title of Manuscript", "Authors", "The Number of Reviews",
                                 "Decision Status"};
    final Object [][] row_data = getRows();
    
    final JTable table = new JTable(row_data, col_names);
    table.getColumn("Title of Manuscript").setCellRenderer(new ButtonRenderer());
    table.getColumn("Title of Manuscript").setCellEditor(new ButtonEditor(my_parent, 
                                                                          new JCheckBox()));
    final JScrollPane scroll_pane = new JScrollPane(table);
    table.setPreferredScrollableViewportSize(new Dimension(500, 500));
    table.setGridColor(Color.BLACK);
    table.setShowGrid(false);
    table.setShowHorizontalLines(true);
    add(scroll_pane, BorderLayout.CENTER);
  }
  
  /**
   * Private method.
   * 
   * @return Returns a 2d array used to fill in the data for the JTable.
   */
  private Object[][] getRows()
  {    
    final List<String> titles = my_manuscripts.getAllManuscriptTitles();
    final List<String> titles_in_conf = new ArrayList<String>();
    for (String s : titles)
    {
//      System.out.println(my_conference);
//      System.out.println(my_manuscripts.getManuscriptByTitle(s).getConference());
      if (my_conference.getConferenceName().equals(my_manuscripts.getManuscriptByTitle(s).getConference().getConferenceName()))
      {
//        System.out.println(s);
        titles_in_conf.add(s);
      }
    }

    final Object[][] table = new Object[titles_in_conf.size()][4];

    for (int key = 0; key < titles_in_conf.size(); key++)
    {
      final Manuscript man = my_manuscripts.getManuscriptByTitle(titles_in_conf.get(key));
      //the title in the first column
      table[key][0] = man.getTitle();
      //places the author in the second column
      table[key][1] = man.getUser().getFullName();
      //places the reviews finished/reviews finished in the third column
      table[key][2] = man.getReviews().size() + "/" +
          man.getNumberOfReviewers();
      //places the status in the 4th column
      if (man.getStatus() < 0)
      {
        table[key][3] = "No";
      }
      else if (man.getStatus() > 0)
      {
        table[key][3] = "Yes";
      }
      else
      {
        table[key][3] = "Undecided";
      }
    }
    return table;
  }
}

//Example code by http://www.java2s.com/Code/Java/Swing-Components/ButtonTableExample.htm
/**
 * Inner class that allows for the construction of button renderers within the JTable. 
 * Code was modeled after example code from 
 * <a href>http://www.java2s.com/Code/Java/Swing-Components/ButtonTableExample.htm</a>
 * 
 * @author Christopher Yeaman
 * @version 12.4.2011
 */
@SuppressWarnings("serial")
class ButtonRenderer extends JButton implements TableCellRenderer 
{

  /**
   * constructor.
   */
  public ButtonRenderer() 
  {
    setOpaque(true);
  }

  /**
   * Retrieves the component in charge of the table cell renderer.
   * 
   * @param the_table The JTable.
   * @param the_value The Object.
   * @param the_selected Boolean value indicating whether the_value is selected.
   * @param the_focus Boolean value indicating whether the table is focused.
   * @param the_row The row concerned.
   * @param the_column The column concerned.
   * 
   * @return The component responsible for the table cell renderer.
   */
  public Component getTableCellRendererComponent(final JTable the_table, 
      final Object the_value, final boolean the_selected, final boolean the_focus, 
      final int the_row, final int the_column) 
  {
    if (the_selected) 
    {
      setForeground(the_table.getSelectionForeground());
      setBackground(the_table.getSelectionBackground());
    } 
    else 
    {
      setForeground(the_table.getForeground());
      setBackground(UIManager.getColor("Button.background"));
    }
    if (the_value == null)
    {
      setText("");
    }
    else
    {
      setText(the_value.toString());
    }
    return this;
  }
}

/**
 * Inner class that allows for the construction of button editors within the JTable. 
 * Code was modeled after example code from 
 * <a href>http://www.java2s.com/Code/Java/Swing-Components/ButtonTableExample.htm</a>
 * 
 * @author Christopher Yeaman
 * @version 12.4.2011
 */
@SuppressWarnings("serial")
class ButtonEditor extends DefaultCellEditor 
{
  /**
   * A Jbutton.
   */
  protected JButton my_button;

  /**
   * A label.
   */
  private String my_label;

  /**
   * Boolean value representing if a button is pushed.
   */
  private boolean my_pushed;
  
  /**  
   * the parent frame.   
   */
  private MainFrame1 my_parent;
  
  /**
   * Constructor.
   * 
   * @param the_parent The parent frame to the panel.
   * @param the_check_box The checkbox.
   */
  public ButtonEditor(final MainFrame1 the_parent, final JCheckBox the_check_box) 
  {
    super(the_check_box);
    my_parent = the_parent;
    my_button = new JButton();
    my_button.setOpaque(true);
    my_button.addActionListener(new ActionListener() 
    {
      public void actionPerformed(final ActionEvent the_event) 
      {
        fireEditingStopped();
      }
    });
  }

  /**
   * Retrieves the component in charge of the table cell editor.
   * 
   * @param the_table The JTable.
   * @param the_value The Object.
   * @param the_selected Boolean value indicating whether the_value is selected.
   * @param the_row The row concerned.
   * @param the_column The column concerned.
   * 
   * @return The component responsible for the table cell renderer.
   */
  public Component getTableCellEditorComponent(final JTable the_table, final Object the_value,
      final boolean the_selected, final int the_row, final int the_column) 
  {
    if (the_selected) 
    {
      my_button.setForeground(the_table.getSelectionForeground());
      my_button.setBackground(the_table.getSelectionBackground());
    } 
    else 
    {
      my_button.setForeground(the_table.getForeground());
      my_button.setBackground(the_table.getBackground());
    }
    if (the_value == null)
    {
      my_label = "";
    }
    else
    {
      my_label = the_value.toString();
    }
    my_button.setText(my_label);
    my_pushed = true;
    return my_button;
  }

  /**
   * Retrieves the label of the cell when it is accessed as if editing.
   * 
   * @return The label when pressed.
   */
  public Object getCellEditorValue() 
  {
    if (my_pushed) 
    {
      my_parent.remove(my_parent.my_panel1);
      my_parent.my_panel2 = new PCDecisionPanel(
        my_parent.getManuscriptCollection().getManuscriptByTitle(my_label), my_parent);
      my_parent.add(my_parent.my_panel2, BorderLayout.CENTER);
      my_parent.pack();
      my_parent.refresh();
    }
    my_pushed = false;
    return new String(my_label);
  }

  /**
   * Stops the cell from being edited.
   * 
   * @return False
   */
  public boolean stopCellEditing() 
  {
    my_pushed = false;
    return super.stopCellEditing();
  }

  /**
   * Stops the editing action from firing.
   */
  protected void fireEditingStopped() 
  {
    super.fireEditingStopped();
  }
}
